package online.shenme.itcast.dao;


import online.shenme.itcast.entity.Question;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class QuestionDao {
    private static final ArrayList<Question> list = new ArrayList<>();

    public static ArrayList<Question> AllQuestion() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("project/src/online/shenme/itcast/file/questions.txt")));
        String s;
        int count=0;
        List<Integer> answerList = null;
        int score = 0;
        int level = 0;
        String title = null;
        List<String> options = null;
        while ((s = br.readLine()) != null) {
            switch (count){
                case 0 :{
                    answerList=new ArrayList<>();
                    String[] split = s.split(",");
                    String[] answers = split[0].substring(split[0].lastIndexOf("=") + 1).split("/");
                    for (String answer : answers) {
                        answerList.add(Integer.parseInt(answer));
                    }
                    score=Integer.parseInt(split[1].charAt(split[1].lastIndexOf("=")+1)+"");
                    level=Integer.parseInt( split[2].substring(split[2].lastIndexOf("=") + 1));
                    count++;
                    break;
                }
                case 1 : {
                    title=s;
                    count++;
                    break;
                }
                case 2:options=new ArrayList<>();
                case 3:
                case 4:{
                    options.add(s);
                    count++;
                    break;
                }
                case 5:{
                    options.add(s);
                    count=0;
                    Question question = new Question(title, options, answerList, score, level);
                    list.add(question);
                    break;
                }
                default:throw new IOException("题库文件内容有误");
            }
        }
        return list;
    }
}
